package cn.hs.service.impl;

import cn.hs.entity.Course;
import cn.hs.entity.UserCourse;
import cn.hs.mapper.CourseMapper;
import cn.hs.mapper.UserCourseMapper;
import cn.hs.mapper.UserMapper;
import cn.hs.service.UserCourseService;
import cn.hs.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserCourseServiceImpl
 * @Description TODO
 * @Author wsh
 * @Date 2024年10月28日 15:48
 * @Version 1.0
 */
@Service
public class UserCourseServiceImpl implements UserCourseService {
  @Autowired
  private UserCourseMapper userCourseMapper;
  @Autowired
  private CourseMapper courseMapper;

    @Override
    public Result insert(UserCourse userCourse) {
        if(userCourse == null){
            return Result.error("收藏失败");
        }
        if(userCourseMapper.selectCountByUserIdAndCourseId(userCourse.getUserId(),userCourse.getCourseId()) == 0){
            int i = userCourseMapper.insertAll(userCourse);
            if(i == 1){
                return Result.success("收藏成功");
            }
            return Result.error();
        }else{
            int delete = userCourseMapper.delete(userCourse.getUserId(), userCourse.getCourseId());
            if(delete == 1){
                return Result.success("已取消收藏");
            }
            return Result.error("取消收藏失败");

        }

    }

    @Override
    public List<UserCourse> findAll(Integer userId) {
        List<UserCourse> list=userCourseMapper.findAll(userId);
        for(int i=0;i<list.size();i++){
            Course course = courseMapper.selectById(list.get(i).getCourseId());
            list.get(i).setTabularChart(course.getTabularChart());
            list.get(i).setCourseName(course.getCourseName());
        }
        return list;
    }
}
